﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;


namespace GPS.Controllers.GPS_Export
{
    public class GPS_DT_QD_CongNhanTNController : Controller
    {
        #region Onit
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(View_Export_DT_QD_CNTN viewModel)
        {
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pDVGTVT_MA);
            viewModel.pDVGTVT_MA = getdvgtvt_Ma;
            ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
        }
        #endregion

        //
        // GET: /ExportHocVien_HopDong/
        #region Index
       
        public ActionResult Index(View_Export_DT_QD_CNTN SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            DataSet ds = RunPKG(SearchModel);
            Export_DT_QD_CNTN _model = new Export_DT_QD_CNTN();
            if (SearchModel.ExportButton == "Xuất Excel")
            {
                if (ds != null)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        _model.DVGTVT_TEN = ds.Tables[0].Rows[i]["DVGTVT_TEN"] == null ? "" : ds.Tables[0].Rows[i]["DVGTVT_TEN"].ToString();
                        _model.DDTTN_QD_SO_QD = ds.Tables[0].Rows[i]["DDTTN_QD_SO_QD"] == null ? "" : ds.Tables[0].Rows[i]["DDTTN_QD_SO_QD"].ToString();
                        if (ds.Tables[0].Rows[i]["DDTTN_QD_NGAY_QD"] != null)
                        {
                            _model.DDTTN_QD_NGAY_QD = Convert.ToDateTime(ds.Tables[0].Rows[i]["DDTTN_QD_NGAY_QD"].ToString());
                        }
                        _model.DS_KH_TEN = ds.Tables[0].Rows[i]["DS_KH_TEN"] == null ? "" : ds.Tables[0].Rows[i]["DS_KH_TEN"].ToString();
                    }

                    for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                    {
                        _model.KH_TEN = ds.Tables[0].Rows[i]["KH_TEN"] == null ? "" : ds.Tables[0].Rows[i]["KH_TEN"].ToString();
                        if (ds.Tables[0].Rows[i]["DTTN_KQTN_NGAYTN"] != null)
                        {
                            _model.DTTN_KQTN_NGAYTN = Convert.ToDateTime(ds.Tables[0].Rows[i]["DTTN_KQTN_NGAYTN"].ToString());
                        }
                        _model.TONGTHISINH = ds.Tables[0].Rows[i]["TONGTHISINH"] == null ? 0 : decimal.Parse( ds.Tables[0].Rows[i]["TONGTHISINH"].ToString());
                    }
                    _model.TENSOGTVT = "NGHỆ AN";
                    _model.DATE = DateTime.Now.ToShortDateString();
                    _model.TIME = DateTime.Now.ToShortTimeString();
                    _model.LISTpRetDSTrungTuyen = COMMON.Helpers.MapDataTableToList<Export_DT_QD_CNTN>(ds.Tables[2]);
                   
                    byte[] contentFile = null;
                    contentFile = GPS.ENTITYS.EXPORTWORD_DT_QD_CNTN.EXPORT_DT_QD_CNTN(Server.MapPath("~/Templates/GPS_Word/Quyetdinhcongnhantotnghiep.docx"), _model, "DocxGenericReport");
                    string nameOutput = string.Format("Quyetdinhcongnhantotnghiep_{0}_{1}.docx",SearchModel.pKH_MA,DateTime.Now.ToShortDateString().Replace("/", "_"));
                    return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
                }
            }
            return View(SearchModel);
        }
       
        #endregion
       
        public DataSet RunPKG(View_Export_DT_QD_CNTN item)
        {
            try
            {
                DataSet ds = OracleHelper.ExecuteDataset(_db.DbContext.Database.Connection.ConnectionString, CommandType.StoredProcedure, "GPS_DAOTAO.GPS_DAOTAO_DTTN_QD_CNTN"
                                                         , new OracleParameter("pDVGTVT_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(item.pDVGTVT_MA) ? DBNull.Value : (object)item.pDVGTVT_MA, ParameterDirection.Input)
                                                         , new OracleParameter("pDTTN_QD_ID", OracleDbType.Int64, string.IsNullOrWhiteSpace(item.pQD_ID) ? DBNull.Value : (object)item.pQD_ID, ParameterDirection.Input)
                                                         , new OracleParameter("pRetHeader", OracleDbType.RefCursor, ParameterDirection.Output)
                                                         , new OracleParameter("pRetTrungTuyen", OracleDbType.RefCursor, ParameterDirection.Output)
                                                         , new OracleParameter("pRetDSTrungTuyen", OracleDbType.RefCursor, ParameterDirection.Output)
                                                        );

                return ds;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}